"""
# -*- coding: utf-8 -*-
# @Time    : 2023/5/24 20:09
# @Author  : 王摇摆
# @FileName: Annoy_Build.py
# @Software: PyCharm
# @Blog    ：https://blog.csdn.net/weixin_44943389?type=blog
"""
import time

from annoy import AnnoyIndex
import numpy as np
from ANN.Data import trains, m, test, d

start = time.time()
# 初始化 AnnoyIndex，使用欧式距离
t = AnnoyIndex(d, 'euclidean')
for i in range(m):
    # 添加样本点
    t.add_item(i, trains[i])
# 构建 20 棵二叉树
t.build(20)
cost = time.time() - start
print("Annoy Build: ", cost)

start = time.time()
# 查询 test 点最近 k 个样本点
nearests, distances = t.get_nns_by_vector(test, k, include_distances=True)
cost = time.time() - start
print("Annoy Search: ", cost)
print("Indexes: ", np.array(nearests))
print("Distances: ", np.array(distances))